package com.rzico.sbl.ui.manager

import android.graphics.drawable.Drawable
import android.view.ViewTreeObserver
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.utils.PictureFileUtils
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityPickupCodeBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.ifNotEmpty
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.helper.BitmapHelper
import com.xinnuo.common.helper.BitmapHelper.getBitmap
import com.xinnuo.common.helper.dp2px
import com.xinnuo.common.helper.longToast
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.glideExt._DrawableImageViewTarget
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import io.reactivex.Flowable

class PickupCodeActivity : BaseActivity(R.layout.activity_pickup_code),
    ViewTreeObserver.OnGlobalLayoutListener {

    private val mBinding by lazy { ActivityPickupCodeBinding.bind(getRootView()) }

    private var mImageHeight = 0
    private var mPickupId = ""
    private var mCupboardId = ""

    override fun getViewModel() = get<CommonViewModel>()

    override fun initData() {
        initTitle(title = intent.getExtra("title"))

        Glide.with(this)
            .load(
                "${BaseUrl.mngPickCod}?shopId=${mPickupId}${
                    mCupboardId.ifNotEmpty { "&cabinetId=${it}" }
                }"
            )
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .transform(RoundedCorners(dp2px(15f)))
            .into(
                object : _DrawableImageViewTarget(mBinding.codeImg) {
                    override fun onLoadStarted(placeholder: Drawable?) {
                        super.onLoadStarted(placeholder)
                        showLoading()
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        super.onLoadFailed(errorDrawable)
                        dismissLoading()
                    }

                    override fun setResource(resource: Drawable?) {
                        super.setResource(resource)
                        dismissLoading()
                    }
                }
            )
    }

    override fun initListener() {
        mPickupId = intent.getExtra("shopId")
        mCupboardId = intent.getExtra("cupboardId")

        mBinding.apply {
            codeImg
                .viewTreeObserver
                .addOnGlobalLayoutListener(this@PickupCodeActivity)

            btAdd.oneClick {
                (mImageHeight > 0)
                    .condition {
                        Flowable.just(mBinding.codeImg)
                            .map {
                                return@map BitmapHelper.saveBitmap(
                                    baseContext,
                                    it.getBitmap(),
                                    PictureFileUtils.createCameraFile(
                                        baseContext,
                                        SelectMimeType.ofImage(),
                                        "", "", ""
                                    )!!
                                )
                            }
                            .compose(applyFlowableSchedulers())
                            .doOnSubscribe { showLoading() }
                            .doFinally { dismissLoading() }
                            .subscribeByFinally(
                                onSuccess = { longToast(it) }
                            )
                    }
            }
        }
    }

    override fun onGlobalLayout() {
        mImageHeight = mBinding.codeImg.measuredHeight
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding
            .codeImg
            .viewTreeObserver
            .removeOnGlobalLayoutListener(this)
    }

}